var globalHost=common.globalHost;
var imageHost=common.imageHost;
var getRewardList=common.getRewardList;
var imageHost=common.imageHost;
var getRewardListUrl=globalHost+getRewardList;
var getBannerList=common.getBannerList;
var getBannerListUrl=globalHost+getBannerList;
var token=common.getToken();
var agentInfo=common.getLocalData('H5-agentInfo');
var getAgentDetailPort=common.getAgentDetailPort;
var getAgentDetailPortUrl=globalHost+getAgentDetailPort;
var $maskControl=$('#mask');
var $alertControl=$('#alert');
var $alertSure=$('#alert-sure');//只有确定按钮的弹窗
var $alertContent=$alertControl.children('.alert-content');
function isAuthAlert(isAuth){
	//点击取消按钮
	 $alertControl.children('.alert-cancel').off('tap').on('tap',function(e){
	    $maskControl.removeClass('mask-show').addClass("mask-hide");
	    $alertControl.removeClass('mask-show').addClass("mask-hide");
	    e.preventDefault();
	    return false;
	});
	//点击确定按钮
	$alertControl.children('.alert-sure').off('tap').on('tap',function(e){
	    $maskControl.removeClass('mask-show').addClass("mask-hide");
	    $alertControl.removeClass('mask-show').addClass("mask-hide");
	    if(isAuth==2){
	        window.location.href="registerFail.html";
	    }else if(isAuth==0){
	        window.location.href="registerInfoAgain.html";
	    }else if(isAuth==1){
	        window.location.href="registerAuth.html";
	    }
	    e.preventDefault();
	    return false;
	});
	//只有确定按钮的不进行操作
	$alertSure.children('.alert-sure').off('tap').on('tap',function(e){
	    $maskControl.removeClass('mask-show').addClass("mask-hide");
	    $alertSure.removeClass('mask-show').addClass("mask-hide");
	    e.preventDefault();
	    return false;
	});
}
/*微信调用扫一扫*/
wx.ready(function() {
    var $scanQRCode=$('#scanQRCode');
    $scanQRCode.off('touchend').on('touchend',function(e){
       // 判断是否是通过验证的经纪人
		common.requestFormData({
			url:getAgentDetailPortUrl,
			sendData:{id:agentInfo.id,token:token},
			resSuccess:function(data){
				var data=data.res.data;
				var isAuth=data['isAuth'];
				if(isAuth==1){
		            $maskControl.removeClass('mask-hide').addClass("mask-show");
		            $alertSure.removeClass('mask-hide').addClass("mask-show");
		            $alertSure.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，请等待审核");
		        }else if(isAuth==3){
		             wxinit._scanQRCode();
		        }else{
		            $maskControl.removeClass('mask-hide').addClass("mask-show");
		            $alertControl.removeClass('mask-hide').addClass("mask-show");
		            $alertControl.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，快去认证吧！")
		        }
				isAuthAlert(isAuth);
			},
			resFailed:function(data){
				
			},
			resError:function(data){
				
			}
		});
    }) 
});
wx.error(function (res) {//错误时调用
    alert(res.errMsg);
});
//type为1表示舍艺新增；type为2表示舍艺推荐；
var sendData={
    token:token,
    type:1
};
var $recentAdd=$('#recentAdd');
var $sheyiCommond=$('#sheyiCommond');
var $recentNum=$('#recentNum');
var $sheyiRecent=$('#sheyiRecent');
var $sheyiMain=$('#sheyiMain');
//最近新增
requestData({
	url:getRewardListUrl,
	sendData:sendData,
	callback:function(data){
		var recordCount=data.res.data.page.recordCount;
		$recentNum.html(recordCount);
		data=data.res.data.record;
		if(!data.length){
			$sheyiRecent.hide();
		}else{
			buildNormalSlider({
				targetSelector:$recentAdd,
				imageHost:imageHost,
				dataList:data
			});
			var swiper = new Swiper('#swiper-recent', {
				freeMode : true,
		        slidesPerView: 'auto',
		        spaceBetween: 0
		    });
		}
	}
});
//舍艺推荐
var sendMain={
	token:token,
    type:2
}
requestData({
	url:getRewardListUrl,
	sendData:sendMain,
	callback:function(data){
		data=data.res.data.record;
		if(!data.length){
			$sheyiMain.hide();
		}else{
			buildNormalSlider({
				targetSelector:$sheyiCommond,
				imageHost:imageHost,
				dataList:data
			});
			var swiper1 = new Swiper('#swiper-commond', {
				freeMode : true,
		        slidesPerView: 'auto',
		        spaceBetween: 0
		    });
		}
	}
});
var $sliderPanel=$(".slider-panel");
requestData({
	url:getBannerListUrl,
	sendData:{token:token,bannerType:1},
	callback:function(data){
		data=data.res.data.record;
		common.buildNormalSwipe({
		    sliderPanel:$sliderPanel,
		    imgPath:imageHost,
		    countingType:"point",
		    data:data,
		    autoPlay:true,
		    moveDelay:3000
		});
	}
});
//舍艺推荐和最近新增模板；
function buildNormalSlider(options){
	var targetSelector=options.targetSelector;
	var dataList=options.dataList;
	var imageHost=options.imageHost;
	var arrTemp=[];
	$.each(dataList,function(idx,ele){
		var viewTimes=Number(ele['viewTimes'])>999?'999+':ele['viewTimes'];
		var attentionTimes=Number(ele['attentionTimes'])>999?'999+':ele['attentionTimes'];
		var tempDOM='<dl class="swiper-slide"><a href="houseDetail.html?id='+ele['rewardId']+'">'+
					'<dt><img src="'+imageHost+ele['coverPhoto']+'">'+
						'<p><i></i><span>'+viewTimes+'</span><i class="like"></i><span>'+attentionTimes+'</span></p>'+
					'</dt>'+
					'<dd class="title-area">'+ele['villageName']+'&nbsp;&nbsp;'+ele['houseFloorArea']+'m²</dd>'+
					'<dd>'+ele['quotePrice']+'万</dd>'+
				'</a></dl>';
		arrTemp.push(tempDOM);
	});
	targetSelector.html(arrTemp.join(""));
}; 
function requestData(options) {
    var sendData=options.sendData;
    var url=options.url;
    var callback=options.callback;
    $.ajax({
        data:sendData,
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        dataType:"json",
        url:url,
        success:function(data){
            data=typeof data==="string"?data=JSON.parse(data):data;
            if(data.code==101 ||data.code==102){
                console.log("token为空或者token失效");
                window.location.href='login.html';
            };
            callback(data);
        },
        timeout:30000,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // changeMsg($notice,"网络不畅，请稍后重试");
                common.errDialog("网络不畅，请稍后重试");
        }
    });
}   